/*
  Copyright (C) 2013 Jolla Ltd.
  Contact: Thomas Perl <thomas.perl@jollamobile.com>
  All rights reserved.

  You may use this file under the terms of BSD license as follows:

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Jolla Ltd nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import QtQuick 2.0
import Sailfish.Silica 1.0
import "../js/API.js" as API

Page {
    id: page
    onStatusChanged: {
        if(status == PageStatus.Active){
            pageStack.pushAttached(Qt.resolvedUrl("About.qml"))
        }
    }
    ListModel{id:doubiModel}
    Component.onCompleted: {
        doubiModel.append(
                        {
                        "answer":"Hi！我就是传说中的小逗比！<br/>"+
                                        "目前主要功能及消息格式如下：<br/>"+
                                        "[调教] 格式:问…答…<br/>"+
                                        "例如:问你好答我很好<br/>"+
                                        "[笑话] 格式:笑话<br/>"+
                                        "[天气] 格式:厦门天气<br/>"+
                                        "[快递] 格式:快递5156236091201<br/>"+
                                        "[百科] 格式:百科机器人<br/>"+
                                        "[糗事] 格式:糗事<br/>"+
                                        "[人品] 格式:人品小逗比<br/>"+
                                        "[翻译] 格式:翻译小逗比<br/>"+
                                        "[报时] 格式:报时 或 时间<br/>"+
                                        "[手机] 格式:手机13838383838<br/>"+
                                        "[QQ号] 格式:QQ 3118409749<br/>"+
                                        "[ip地址] 格式:ip 115.47.22.2<br/>"+
                                        "[藏头诗] 格式:藏头诗大象无形<br/>"+
                                        "[查拼音] 格式:拼音逗 或 逗拼音<br/>"+
                                        "[抽签] 格式:抽签",
                        "isrobot":1
                    });
    }
    PageHeader {
        id:header
        title: "小黄鸡"
    }
    SilicaListView {
        id: listView
        clip:true
        model: doubiModel
        quickScroll:true
        anchors{
            left:parent.left
            right:parent.right
            top:header.bottom
        }
        MouseArea{
            anchors.fill: parent
            onClicked: {
                listView.focus=true;
            }
        }
        height: page.height-header.height-Theme.fontSizeMedium-50
        //contentHeight:childrenRect.height
        delegate:Item {
            id: delegate
            height: childrenRect.height+5
            width: parent.width
            Image{
                id:touxiang
                visible: (isrobot === 1?true:false)
                source:"../img/xiaoji.png"
                width: 50; height: 50
                anchors{
                    left:parent.left
                    leftMargin: 15
                    top:parent.top
                    topMargin: 10
                }
            }

            Label {
                id:chatMes
                visible: (isrobot === 1?true:false)
                x: Theme.paddingLarge
                text:"  "+answer
                wrapMode: Text.WordWrap
               font.pixelSize: Theme.fontSizeExtraSmall*6/5
                anchors {
                    left: touxiang.right;
                    top:touxiang.top
                    right: parent.right
                    leftMargin: 15
                    rightMargin:15
                    topMargin: 10
                }
                color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
            }
            Image{
                id:touxiang2
                visible: (isrobot === 0?true:false)
                source:"../img/ted.png"
                width: 50; height: 50
                anchors{
                    right:parent.right
                    rightMargin: 15
                    top:parent.top
                    topMargin: 10
                }
            }

            Label {
                id:chatMes2
                visible: (isrobot === 0?true:false)
                x: Theme.paddingLarge
                text:answer+"  "
                font.pixelSize: Theme.fontSizeExtraSmall*6/5
                wrapMode: Text.WordWrap
                anchors {
                    right: touxiang2.left;
                    top:touxiang2.top
                    leftMargin: 15
                    rightMargin:15
                    topMargin: 10
                }
                color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
            }
            Separator {
                visible:(index > 0?true:false)
                width:parent.width;
                color: Theme.highlightColor
            }
        }
        VerticalScrollDecorator {flickable: listView}


    }



    Column {
        id: footerComponent
        y:page.height - 50
        anchors { left: parent.left; right: parent.right }
        TextField {
            id: searchField
            width: parent.width-clearButton.width
            //icon.source: "image://theme/icon-cover-next"
            label: "-->在这里输入"
            placeholderText: label
            horizontalAlignment: TextInput.AlignLeft
           // focusOutBehavior: FocusBehavior.KeepFocus
            EnterKey.text:  "逗它"
            EnterKey.enabled: true
            //EnterKey.highlighted: highlighted.checked
            EnterKey.onClicked: {
                if(searchField.text){
                     parent.focus = true;
                    API.chat(searchField.text);
                    searchField.text="";
                    searchField.label="-->在这里输入";
                    listView.positionViewAtEnd();
                }else{
                    searchField.label="请输入内容";
                }
            }
        }
        IconButton {
            id: clearButton
            anchors {
                right: parent.right
                rightMargin: Theme.paddingLarge
            }
            width: icon.width
            height: icon.height
            icon.source: "image://theme/icon-m-clear"
            enabled: (toString(searchField.text).length>0?true:false)
            onClicked: {
                searchField.text="";
                parent.focus = true;
            }
        }
    }


}





